﻿module Sustainalytics.Insights {

    import Common = Sustainalytics.Common;

    export class InsightsListController {

        public insightsTopList: any[] = new Array;

        // order by column
        public insightsOrderByCol: string;

        // order by direction
        public insightsOrderByColAsc: boolean;
        public lastSkip: number = 0;
        public take: number = 10;
        public lastItemsCount: number = 0;
        private insightsContainer;

        //Ctor.
        constructor(
            private $rootScope: ng.IScope,
            private $scope: ng.IScope,
            private $q: ng.IQService,
            private $location: ng.ILocationService,
            private InsightsService: InsightsService,
            private notificationFactory: Common.INotificationFactory) {

            var ctx = this;

            $scope['insightsList'] = ctx;
            ctx.insightsOrderByCol = 'DatePublished';
            ctx.insightsOrderByColAsc = false;
            ctx.insightsTopList = new Array();
            ctx.lastSkip = 0;
            ctx.getInsights();
            ctx.findInDom();
            ctx.addItemsOnScroll();

        }

        public getInsights(): ng.IPromise<{}> {

            var ctx = this;
            ctx.$rootScope['loading'] = true;
            ctx.insightsOrderByCol = 'DatePublished';
            ctx.insightsOrderByColAsc = false;

            var promiseGet: ng.IPromise<InsightDto[]> = ctx.InsightsService.getInsightsList(ctx.lastSkip, ctx.take, [ctx.insightsOrderByCol], ctx.insightsOrderByColAsc);

            //on success
            promiseGet
                .then(function (lst: any[]) {
                    ctx.lastSkip += ctx.take;
                    if (lst) {
                        $(lst).each(function () {
                            ctx.insightsTopList.push(this);
                        });
                    }
                })
                ['finally'](function () {
                    ctx.$rootScope['loading'] = false;
                });
            return promiseGet;
        }

        private findInDom() {
            var ctx = this;
            ctx.insightsContainer = $('#insightsContainer');// ctx.element.find('#newsContainer');
        }

        private addItemsOnScroll() {
            var ctx = this;
            var canRequestData = true;

            // handle scroll
            ctx.insightsContainer.on('scroll', function (ev) {
                // get scrollbar element
                var target = $(ev.target);

                // check if it reached the max scroll size and go to next page
                if (target[0].scrollHeight == (target.scrollTop() + target.outerHeight())) {
                    if (canRequestData) {
                        canRequestData = false;
                        ctx.$scope.$apply(function () {
                            ctx.getInsights().then(_ => canRequestData = true);
                        });
                    }
                }
            });
        }
    }


    // register controller with Insights Angular module
    angular.module('Sustainalytics.Insights').controller('InsightsListController',
        ['$rootScope', '$scope', '$q', '$location', 'InsightsService', 'notificationFactory', InsightsListController]);

}